<template>
  <el-button
    type="primary"
    size="default"
    :icon="Plus"
    @click="emit('setState', 0)"
  >添加SPU
  </el-button>
  <el-table :data="spuPageList" border stripe :style="{ margin: '30px 0' }">
    <el-table-column type="index" width="80" label="序号" />
    <el-table-column label="SPU名称" prop="spuName"></el-table-column>
    <el-table-column label="SPU描述" prop="description"></el-table-column>
    <el-table-column label="操作">
      <template v-slot="{ row, $index }">
        <el-button
          type="primary"
          size="small"
          title="添加SKU"
          @click="addSku(row)"
          icon="ele-Plus"
        ></el-button>
        <el-button
          type="primary"
          size="small"
          title="编辑SPU"
          @click="emit('setState', 0, row)"
          icon="ele-Edit"
        ></el-button>
        <el-button
          type="info"
          size="small"
          title="查看SKU"
          @click="findSkuList(row)"
          icon="ele-Warning"
        ></el-button>
        <el-button
          type="danger"
          size="small"
          title="删除SPU"
          @click="deleteSpu(row.id)"
          icon="ele-Delete"
        ></el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
    v-model:page-size="limit"
    v-model:current-page="page"
    :total="total"
    :page-sizes="[3, 5, 7, 9]"
    :pager-count="5"
    layout="prev,pager,next,sizes,jumper,total"
    background
  />
  <el-dialog title="sku列表" v-model="dialogTableVisible">
    <el-table  v-loading="loading" :data="skuList"  border stripe>
      <el-table-column label="序号" type="index" width="80" />
      <el-table-column prop="skuName" label="名称"></el-table-column>
      <el-table-column prop="price" label="价格"></el-table-column>
      <el-table-column  label="默认图片">
        <template v-slot="{row}">
          <img height="120" :src="row.skuDefaultImg" alt="" >
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent } from "vue";

export default defineComponent({
  name: "SpuList"
});
</script>

<script setup lang="ts">
import { Plus } from "@element-plus/icons-vue";
import { ref, toRefs, watch } from "vue";
import type { skuInfoListType } from "@/api/sku";
import { requestFindBySpu } from "@/api/sku";
import { requestSpuList, requestDeleteSpu, type spuInfoType } from "@/api/spu";
import useAttrStore from "@/stores/attr";
import { storeToRefs } from "pinia";
import { ElMessage } from "element-plus";
//使用defineEmits拿到自定义事件
const emit = defineEmits<{
  (event: "setState", state: number, row?: spuInfoType): void;
}>();
//实例化attr,拿到categoryId用于发送请求
const attrStore = useAttrStore();

/**@spuPageList spu列表*/
const spuPageList = ref<spuInfoType[]>([]);

const { category3Id } = storeToRefs(attrStore);

/**@pageData 页码数据*/
const pageData = ref({ page: 1, limit: 3, total: 0 });

//将页码数据解构
const { page, limit, total } = toRefs(pageData.value);

/**@getSpuPageList spu列表请求的事件函数*/
const getSpuPageList = async () => {
  try {
    const result = await requestSpuList(
      page.value,
      limit.value,
      category3Id.value as number
    );
    spuPageList.value = result.records;
    total.value = result.total;
  } catch (e) {
    ElMessage.success("");
  }
};
//三级列表改变的时候获取spu列表
watch(
  [category3Id, page, limit],
  () => {
    if (category3Id.value === null) {
      spuPageList.value = [];
      total.value = 0;
      return;
    }
    try {
      getSpuPageList();
    } catch (e) {
      ElMessage.error("spu列表请求失败");
    }
  },
  { immediate: true }
);
/**@deleteSpu 点击删除某个spu*/
const deleteSpu = async (spuId: number) => {
  try {
    await requestDeleteSpu(spuId);
    await getSpuPageList();
  } catch (e) {
    ElMessage.error("删除数据失败");
  }
};

/**@addSku 点击去往新增sku*/
const addSku = (row: spuInfoType) => {
  emit("setState", 1, row);
};
// 点击查看sku列表按钮
const dialogTableVisible = ref(false);
// 加载
const loading = ref(false);

//查看sku列表
const skuList=ref<skuInfoListType>([])
const findSkuList = async (row: spuInfoType) => {
  dialogTableVisible.value = true;
  try {
    const result = await requestFindBySpu(row.id as number);
    skuList.value=result
  }catch (e){
    ElMessage.error('查看sku列表失败')
  }
};
</script>
